package com.huluobo.user.view

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.UIState
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import com.huluobo.user.entity.ApproveBeanItem
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 *  Created by LC on 2025/2/13.
 */
@Composable
fun SPPage(navHostController: NavHostController, json: String, vm: UserViewModel = hiltViewModel()) {
    val entity = Gson().fromJson(json, ApproveBeanItem::class.java)

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.onError -> {
                        Log.e("TagA", "审批失败:${it.msg}")
                    }

                    UIState.onLoading -> {}
                    is UIState.onSuccess<*> -> {
                        ToastUtils.showLong("审批成功")
                        navHostController.popBackStack()
                    }
                }
            }
        }
    })

    Column(Modifier.padding(horizontal = 20.dp, vertical = 10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
        BaseTitle(title = "审批详情", navHostController = navHostController)
        Text(text = "申报人信息", color = Color.Blue)
        Card(
            border = BorderStroke(1.dp, Color.LightGray),
            colors = CardDefaults.cardColors(containerColor = Color.White),
            modifier = Modifier.fillMaxWidth()
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
            ) {
                AsyncImage(
                    model = "https://img0.baidu.com/it/u=788800651,2030194952&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1730307600&t=9c17b588a934b0ccf76349e5e5b7ca08",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(20.dp))
                Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = entity.staffName)
                    Text(text = entity.deptName)
                }
            }
        }

        Spacer(modifier = Modifier.height(10.dp))
        Text(text = "请假详情", color = Color.Blue)
        Text(text = "请假方式 : ${entity.type}")
        Text(text = "开始时间 : ${entity.startTime}")
        Text(text = "结束时间 : ${entity.endTime}")
        Text(text = "请假时长 : ${entity.hours}小时")
        Text(text = "请假原因 : ${entity.reason}")

        Text(text = "审批意见", color = Color.Blue)
        Divider()
        var status by remember {
            mutableStateOf("通过")
        }
        Row(
            Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            Text(text = "结果 : ")
            RadioButton(selected = status == "通过", onClick = { status = "通过" })
            Text(text = "通过")
            RadioButton(selected = status == "拒绝", onClick = { status = "拒绝" })
            Text(text = "拒绝")
        }

        var comReason by remember {
            mutableStateOf("")
        }
        Row(Modifier.fillMaxWidth()) {
            Text(text = "意见 : ")
            OutlinedTextField(value = comReason, onValueChange = { comReason = it }, maxLines = 3)
        }

//        {
//            "id": 0,
//            "status": "",
//            "comTime": "",
//            "comReason": ""
//        }
        Button(onClick = {
            val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
            val map = mapOf(
                "id" to entity.id,
                "status" to status,
                "comReason" to comReason,
                "comTime" to time
            )
            vm.sendIntent(UserIntent.approve(map))

        }) {
            Text(text = "提交")
        }
    }
}